package player;

import java.util.ArrayList;
import java.util.List;

public class PlayList {
    private String playListName;//播放列表名称
    private List<Song> musicList;//播放列表的歌曲集合

    //构造方法
    public PlayList(String playListName) {
        this.playListName = playListName;
        //创建一个播放列表的歌曲集合
        musicList=new ArrayList<Song>();
    }

    public String getPlayListName() {
        return playListName;
    }

    public void setPlayListName(String playListName) {
        this.playListName = playListName;
    }

    public List<Song> getMusicList() {
        return musicList;
    }

    public void setMusicList(List<Song> musicList) {
        this.musicList = musicList;
    }

    //将歌曲添加到播放列表
    public void addToPlayList(Song song){
        //要排除重复添加的情况
        boolean flag=false;//判断播放列表中的歌曲是否存在
        for (Song song1:musicList){
            if(song1.equals(song)){
                flag=true;
                break;
            }
        }
        if(flag){
            System.out.println(song.getId()+"该歌曲已经存在于播放列表中!");
        }else {
            musicList.add(song);
        }
    }

    //通过歌曲id查询
    public Song searchSongById(String id){
        Song song=null;
        for (Song song1:musicList){
            if (song1.getId().equals(id)){
                //如果相等就找到了
                song=song1;
                break;
            }
        }
        return song;
    }
    //通过歌曲名称查询
    public Song searchSongByName(String name){
        Song song=null;
        for (Song song1:musicList){
            if (song1.getName().equals(name)){
                //如果相等就找到了
                song=song1;
                break;
            }
        }
        return song;
    }

    //修改歌曲信息
    public void updateSong(String id,Song song){
        //根据id查询到相关的歌曲信息,然后进行修改
        Song song1=searchSongById(id);
        if (song1==null){
            System.out.println("没有找到id为"+id+"对应的歌曲信息!");
        }else {
            //先移除原来的信息,再重新添加
            musicList.remove(song1);
            musicList.add(song);
            System.out.println("修改成功!");
        }
    }
    //根据id删除播放列表中的指定歌曲信息
    public void deleteSong(String id){
        Song song=searchSongById(id);
        if (song!=null){
            musicList.remove(song);
        }else {
            System.out.println("没有找到id为"+id+"对应的歌曲信息!");
        }
    }
    //显示播放列表中的所有歌曲
    public void displayAllSong(){
        System.out.println("播放列表中的所有歌曲为:");
        for (Song song:musicList){
            System.out.println(song);
        }
    }
}
